class Solution {
public:
    int minOperations(vector<int>& nums) {
        int cnt = 0;
        int top = -1;
        for (int x : nums) {
            while (top >= 0 && x < nums[top]) {
                cnt++;
                top--;
            }
            if (top < 0 || x != nums[top])
                nums[++top] = x;
        }
        return cnt + top + (nums[0] > 0);
    }
};